package com.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Objects;

import sun.misc.BASE64Encoder;

public class ImageUtil {

	public static void main(String[] args) {
		// 第一个:把网络图片装换成Base64
//		String netImagePath = "https://carmarket.oss-cn-beijing.aliyuncs.com/indexad.png";
//		// 下面是网络图片转换Base64的方法
//		
//		System.out.print(NetImageToBase64(netImagePath));
		// 下面是本地图片转换Base64的方法
		String imagePath = "d://dhead.png";
////		ImageToBase64(imagePath);
////		System.out.println(ImageToBase64(imagePath));
		System.out.print(byteConverterBASE64(imagePath));
	}

	
	public static String byteConverterBASE64(String img){

	    File file=new File(img);
	    long size = file.length();
	    byte[] imageByte = new byte[(int)size];
	    FileInputStream fs = null;
	    BufferedInputStream bis = null;
	    try {
	        fs = new FileInputStream(file);
	        bis = new BufferedInputStream(fs);
	        bis.read(imageByte);
	    } catch (Exception e) {
	    	System.out.println("fail!");
//	        log.error("byte转换BASE64出错："+e.getMessage());
	    } finally{
	        if(bis != null){
	            try {
	                bis.close();
	            } catch (IOException e) {
	            	System.out.println("关闭输入流出错："+e.getMessage());
	            }
	        }
	        if(fs != null){
	            try {
	                fs.close();
	            } catch (IOException e) {
	            	System.out.println("关闭输入流出错："+e.getMessage());
	            }
	        }
	    }
	    return (new sun.misc.BASE64Encoder()).encode(imageByte);    
	}
	
	/**
	 * 本地图片转换Base64的方法
	 * 
	 * @param imgPath
	 *                
	 */

	public static String ImageToBase64(String imgPath) {
		byte[] data = null;
		// 读取图片字节数组
		try {
			InputStream in = new FileInputStream(imgPath);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 对字节数组Base64编码
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(Objects.requireNonNull(data));
	}

	/**
	 * 网络图片转换Base64的方法
	 * 
	 * @param netImagePath
	 *                
	 */
	public static String NetImageToBase64(String netImagePath) {
		String res="";
		final ByteArrayOutputStream data = new ByteArrayOutputStream();
		try {
			// 创建URL
			URL url = new URL(netImagePath);
			final byte[] by = new byte[1024];
			// 创建链接
			final HttpURLConnection conn = (HttpURLConnection) url
					.openConnection();
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(5000);

			try {
				InputStream is = conn.getInputStream();
				// 将内容读取内存中
				int len = -1;
				while ((len = is.read(by)) != -1) {
					data.write(by, 0, len);
				}
				// 对字节数组Base64编码
				BASE64Encoder encoder = new BASE64Encoder();
				res = encoder.encode(data.toByteArray());
				// 关闭流
				is.close();
				
			} catch (IOException e) {
				e.printStackTrace();
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}

}
